//
//  MHPoliciesViewController.m
//  MHO
//
//  Created by papo on 4/26/14.
//  Copyright (c) 2014 maihoang. All rights reserved.
//

#import "MHPoliciesViewController.h"
#import "Lib.h"
#import "MHNavibarView.h"

@interface MHPoliciesViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (nonatomic, strong) MHNavibarView* navibarView;
@end

@implementation MHPoliciesViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _navibarView = [MHNavibarView createWithTitle:@"Điều khoản sử dụng" leftButton:nil rightButton:nil searchButton:NO];
    _navibarView.rightButton.hidden = YES;
    [_navibarView setActionSelectorDelegate:self left:@selector(leftButtonPressed:) right:nil search:nil];
    [_navibarView setRootViewController:self];
    [self.view addSubview:_navibarView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated {
    [Lib showLoadingView];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString* ret = [ServiceLib sendGetRequest:@"http://api.mho.vn/services/Config?type=mho_policies"];
        NSString* string;
        if (ret) {
            SBJsonParser* parser = [[SBJsonParser alloc] init];
            id dict = [parser objectWithString:ret];
            if (dict) {
                id data = [dict objectForKey:@"Data"];
                id value = [data objectForKey:@"Value"];
                string = value;
            }
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            [Lib removeLoadingView];
            [self.webView loadHTMLString:string baseURL:nil];
        });
    });
}

- (void)loadHtmlString:(NSString *)string url:(NSURL *)url {
    [self.webView loadHTMLString:string baseURL:url];
}

- (void)leftButtonPressed:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
